package org.makeprogress.springbootbase.controller;

import org.makeprogress.springbootbase.delegate.TestDelegate;
import org.makeprogress.springbootbase.model.Result;
import org.makeprogress.springbootbase.model.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;


/**
 * @author liuyi
 * @create 2019-09-19 15:23
 **/

@Controller
@Validated
public class TestController {
    @Autowired
    TestDelegate testDelegate;

    @RequestMapping("/test")
    public String test() {
        return "view/test.html";
    }

    @GetMapping("/testget")
    @ResponseBody
    public Result<String> index(@RequestParam @NotEmpty() String name, @RequestParam @Min(1) @Max(10) Integer id) {
        System.out.println(name);
        System.out.println(id);
        return testDelegate.index();
    }

    @GetMapping("/testget/{id}")
    @ResponseBody
    public Result<String> get(@PathVariable @Min(1) @Max(10) Integer id) {
        System.out.println(id);
        return testDelegate.index();
    }

    @PostMapping("/testPost")
    @ResponseBody
    public Result<String> index(@Valid @RequestBody Test test) {
        System.out.println(test.getId());
        System.out.println(test.getName());
        return testDelegate.index();
    }

    @PostMapping("/testPostFormdata")
    @ResponseBody
    public Result<String> testPostFormdata(@Valid Test test) {
        System.out.println(test.getId());
        System.out.println(test.getName());
        return testDelegate.index();
    }

    @PutMapping("/testPut")
    @ResponseBody
    public Result<String> test2(@Valid @RequestBody Test test) {
        System.out.println(test.getId());
        System.out.println(test.getName());
        return testDelegate.index();
    }

    @DeleteMapping("/testDelete")
    @ResponseBody
    public Result<String> testDelete(@Valid @RequestBody Test test) {
        System.out.println(test.getId());
        System.out.println(test.getName());
        return testDelegate.index();
    }
}